//
//  MyZTTableViewController.m
//  CarTown
//
//  Created by xionghao on 15/8/25.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "MyZTTableViewController.h"
#import "MyZTHeaderView.h"
#import "jianjieTableViewCell.h"
#import "jianjieframe.h"
#import "ZTCarTitleTableViewCell.h"
#import "ZTPingpaiTableViewCell.h"
#import "ZTCarTableViewCell.h"
#import "PingpaiMode.h"
#import "MyAllZCViewController.h"
#import "ShareView.h"
#import "Puser.h"
#import "ZTResdMode.h"
#import "ZTModel.h"
#import "PinpaiCellMode.h"
#import "ZTTitleModel.h"
#import "ZTJJmode.h"
#import "ZCModel.h"
#import "LCActionSheet.h"
#import <UIImageView+WebCache.h>
#import "UserInfoUpdateResde.h"
#import "IWUploadParam.h"
#import "ZTEidtViewController.h"
#import "JianjieViewController.h"
#import "addTableViewController.h"
#import "MyZTCarDetailTableViewController.h"
#import "UMSocial.h"
#import "UserInfoUpdateResde.h"
#import "UINavigationItem+Loading.h"
#import "MLImageCrop.h"
#import "ZTTableCellTitle.h"
#import "ZTActivityTableViewCell.h"
#import "ZTGetCarTableViewCell.h"
#import "AttenZTTableViewCell.h"
#import "ZTOtherTitleMode.h"
#import "ZTActivityModel.h"
#import "ZTGetCarModel.h"
#import "ZTAttenCar.h"
#import "MYAttenZTViewController.h"
#import "MyActivityListViewController.h"
#import "MYActivityDetailViewController.h"
#import "ZTAttenTitlemode.h"
#import "ZTAttenTitleTableViewCell.h"
#import "SearchTableViewController.h"
#import "IndexNavViewController.h"
#import "OhterActiviyListTableViewController.h"
#import "NoZTActivityModel.h"
#import "NoItemTableViewCell.h"
#import "PostActivityTableViewController.h"

@interface MyZTTableViewController ()<ZTPingpaiClickDelegate,ShareclickDelegate,MyZtHeaderViewDelegate,LCActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,ZTEditViewControllerDelegate,MLImageCropDelegate,UIAlertViewDelegate,UITextFieldDelegate>{
    BOOL _isshow;
    int _rows;
    BOOL _isPicker;
}
@property (nonatomic,strong) MyZTHeaderView *header;
@property (nonatomic,strong) jianjieframe *frame;
@property (nonatomic,assign) CGFloat newheight;
@property (nonatomic,assign) NSInteger selectrow;
@property (nonatomic,strong) UIButton *addbtn;
@property (nonatomic,strong) NSMutableArray *pingpaiarray;
@property (nonatomic,strong) NSMutableArray *dataarray;
@property (nonatomic,strong) NSMutableArray *activityarray;
@property (nonatomic,strong) NSMutableArray *jnarray;
@property (nonatomic,strong) NSMutableArray *attenarray;
@property (nonatomic,strong) Puser *user;
@property (nonatomic,strong) UIImagePickerController *picker;
@property (nonatomic,assign) BOOL isfromSearch;
@property (nonatomic,strong) ZTResdMode *mode;
@property (nonatomic,strong) ZTJJmode *ztjj;
@property (nonatomic,assign) BOOL needupdate;

@end

@implementation MyZTTableViewController

-(NSMutableArray *)attenarray{
    if ( _attenarray==nil) {
        _attenarray=[NSMutableArray array];
    }
    return  _attenarray;
}
-(NSMutableArray *)activityarray{
    if ( _activityarray==nil) {
        _activityarray=[NSMutableArray array];
    }
    return  _activityarray;
}
-(NSMutableArray *)jnarray{
    if ( _jnarray==nil) {
        _jnarray=[NSMutableArray array];
    }
    return  _jnarray;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField{
    [textField endEditing:YES];
    
    
    SearchTableViewController *svc=[SearchTableViewController viewController];
    
    svc.isFromSearchZT=YES;
    IndexNavViewController *navc=[[IndexNavViewController alloc] initWithRootViewController:svc];
  
    [self presentViewController:navc animated:YES completion:nil];
}
+(instancetype)viewcontroller{
    MyZTTableViewController *myztvc=[[MyZTTableViewController alloc]initWithStyle:UITableViewStyleGrouped];
    myztvc.hidesBottomBarWhenPushed=YES;
    return myztvc;
}
-(void)shareclickWithIndex:(NSInteger)index{
    ZTModel *mode=self.mode.data;
    UMSocialUrlResource *resource=[[UMSocialUrlResource alloc]initWithSnsResourceType:UMSocialUrlResourceTypeImage url:[NSString getImageURLstr:mode.tx]];
    [UMSocialData defaultData].extConfig.wxMessageType =UMSocialWXMessageTypeWeb;
    NSString *gourl;
    if (self.isfromSearch) {
        if (self.user_id) {
            gourl=[NSString stringWithFormat:@"%@%@%@",websitehosturl,@"/modules.php?app=car_display&user_id=",self.user_id];
        }else{
            gourl=[NSString stringWithFormat:@"%@%@%@",websitehosturl,@"/modules.php?app=car_display&com_id=",self.com_id];
        }
        
    }else{
        if ([self.user.power integerValue]<5) {
            gourl=[NSString stringWithFormat:@"%@%@%@",websitehosturl,@"/modules.php?app=car_display&user_id=",self.user.user_id];
        }else{
            gourl=[NSString stringWithFormat:@"%@%@%@",websitehosturl,@"/modules.php?app=car_display&com_id=",self.user.com_id];
        }
        
    }
    
    
    switch (index) {
        case 0:
        {
            
            [UMSocialData defaultData].extConfig.wechatSessionData.url = gourl;
            
            
            
            [UMSocialData defaultData].extConfig.wechatSessionData.title =[NSString stringWithFormat:@"%@",mode.title];
            
            [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToWechatSession] content:mode.msg image:nil location:nil urlResource:resource presentedController:self completion:^(UMSocialResponseEntity *response){
                if (response.responseCode == UMSResponseCodeSuccess) {
                    NSLog(@"分享成功！");
                }
            }];
        }
            break;
        case 1:
        {
            [UMSocialData defaultData].extConfig.wechatTimelineData.url = gourl;
            
            [UMSocialData defaultData].extConfig.wechatTimelineData.title =[NSString stringWithFormat:@"%@",mode.title];
            
            [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToWechatTimeline] content:mode.msg image:nil location:nil urlResource:resource presentedController:self completion:^(UMSocialResponseEntity *response){
                if (response.responseCode == UMSResponseCodeSuccess) {
                    NSLog(@"分享成功！");
                }
            }];
        }
            break;
            
        case 2:
        {
            
            
            
        }
            break;
            
        default:
            break;
    }
    
    
    
    
}

-(void)pingpaiItemClick:(NSInteger)index{
    
    MyAllZCViewController *myzc=[MyAllZCViewController Viewcontroller];
    if (self.isfromSearch) {
        myzc.user_id=self.user_id;
        myzc.com_id=self.com_id;
        myzc.isFromSearch=self.isfromSearch;
    }
    PingpaiMode *brandmode=self.mode.data.brand[index];
    myzc.brand_id=brandmode.brand_id;
    myzc.ztmode=self.mode.data;
    [self.navigationController pushViewController:myzc animated:YES];
    
}

-(void)headbanclick:(NSInteger)index{
    if (index<2&&!self.isfromSearch) {
        LCActionSheet *sheet=[[LCActionSheet alloc] initWithTitle:@"更换背景图片" buttonTitles:@[@"拍照",@"从相册中选择"] redButtonIndex:-1 delegate:self];
        sheet.tag=index;
        
        [sheet show];
    }else{
        switch (index) {
            case 2:
                [self posttoEdit:ZTNAMETYPE];
                break;
            case 3:
                [self posttoEdit:ZTADDRESSTYPE];
                break;
            case 4:
                [self posttoEdit:ZTPHONETYPE];
                break;
            case 5:
                [self posttoEdit:ZTTITLETYPE];
                break;
            default:
                break;
        }
    }
    
}


-(void)posttoEdit:(ZTEidtViewType)index{
    if ((self.user_id&&[self.user_id integerValue]==[self.user.user_id integerValue])||(self.com_id&&[self.com_id integerValue]==[self.user.com_id integerValue])) {
        [self pushto:index];
        
    }else{
        
        if (self.isfromSearch) {
            
            [self telphone];
            return;
        }else{
            [self pushto:index];
        }
        
    }
    
}
-(void)PostOK:(NSString *)string withtype:(ZTEidtViewType)type{
    
    switch (type) {
        case 0:
            self.header.leftlable.text=string;
            self.mode.data.username=string;
            break;
        case 1:
            
            break;
        case 2:
            self.header.midlable.text=string;
            self.mode.data.addr=string;
            
            break;
        default:
            break;
    }
}
-(void)pushto:(NSInteger)tag{
    ZTEidtViewController *vc=[ZTEidtViewController viewcontroller];
    vc.type=tag;
    vc.delegate=self;
    switch (tag) {
        case ZTADDRESSTYPE:
            
            vc.text=self.mode.data.addr;
            
            break;
        case ZTPHONETYPE:
            vc.text=self.mode.data.mobile;
            
            break;
        case ZTNAMETYPE:
            vc.text=self.mode.data.username;
            break;
        case ZTTITLETYPE:
            vc.text=self.mode.data.title;
            break;
        default:
            break;
    }
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==0) {
        return;
    }else{
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",self.mode.data.mobile]]];
    }
}

-(void)telphone{
    
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"确定拨打此电话" message:self.mode.data.mobile delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];
}

-(void)actionSheet:(LCActionSheet *)actionSheet didClickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==actionSheet.btnCount-1) return;
    if (buttonIndex==0) {
        [self showCarmera:actionSheet.tag];
    }else{
        [self showimagepiker:actionSheet.tag];
    }
}

-(NSMutableArray *)dataarray{
    if ( _dataarray==nil) {
        _dataarray=[NSMutableArray array];
    }
    return  _dataarray;
}

-(NSMutableArray *)pingpaiarray{
    if (_pingpaiarray==nil) {
        _pingpaiarray=[NSMutableArray array];
        
    }
    return _pingpaiarray;
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    CGFloat y=-self.tableView.contentOffset.y;
    if (!_isPicker) {
        [self getdatafromWeb];
    }
    
    if (y<0) {
        [self setupNav:NO];
    }else{
        [self setupNav:YES];
    }
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    self.tableView.bounces=NO;
    if (self.isFromSUS) {
        NSMutableArray *vcarray=[[NSMutableArray alloc] initWithArray: self.navigationController.viewControllers];
        [vcarray removeObjectAtIndex:self.navigationController.viewControllers.count-2];
        [vcarray removeObjectAtIndex:self.navigationController.viewControllers.count-3];
        self.navigationController.viewControllers = vcarray;
    }
    if (self.isFromALL) {
        NSMutableArray *vcarray=[[NSMutableArray alloc] initWithArray: self.navigationController.viewControllers];
        [vcarray removeObjectAtIndex:self.navigationController.viewControllers.count-2];
        
        self.navigationController.viewControllers = vcarray;
    }
    
    
    
    
    
    
    MyZTHeaderView *header=[MyZTHeaderView header];
    
    header.frame=CGRectMake(0, 0, XHScreenwidth, XHScreenwidth==320?192:222);
    [self.tableView insertSubview:header atIndex:0];
    self.header=header;
    self.header.delegate=self;
    self.tableView.contentInset=UIEdgeInsetsMake(0, 0, 0, 0);
    self.tableView.tableHeaderView=header;
    self.automaticallyAdjustsScrollViewInsets=NO;
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"top-r-two"] style:UIBarButtonItemStyleDone target:self action:@selector(share)];
    if (self.user_id||self.com_id) {
        self.isfromSearch=YES;
    }else{
        self.isfromSearch=NO;
    }
    [self setuptablview];
    [self setupvist];
    
    _isshow=NO;
    
    
}

-(void)setupvist{
    if (self.isfromSearch) {
        NSMutableDictionary *params=[NSMutableDictionary dictionary];
        params[@"action"]=@"visit";
        params[@"did"]=self.user_id?self.user_id:self.com_id;
        params[@"dtype"]=self.user_id?@"0":@"1";
        params[@"user_id"]=[self.user.power integerValue]<5?self.user.user_id:self.user.com_id;
        params[@"type"]=[self.user.power integerValue]<5?@"person":@"company";
        
        [PostUitls Post:mynewzturl parameters:params success:^(id response) {
            
        } failure:^(NSError *error) {
            
        }];
        
    }
}





-(UIImagePickerController *)picker{
    if (_picker==nil) {
        _picker=[[UIImagePickerController alloc]init];
        _picker.view.backgroundColor = [UIColor whiteColor];
        
        _picker.delegate =self;
        
        
    }
    return _picker;
}
-(void)showCarmera:(NSInteger)tag{
    UIImagePickerControllerSourceType sourcheType = UIImagePickerControllerSourceTypeCamera;
    self.picker.view.tag=tag;
    self.picker.sourceType=sourcheType;
    [self presentViewController:self.picker animated:YES completion:nil];
}
-(void)showimagepiker:(NSInteger)tag{
    UIImagePickerControllerSourceType sourcheType = UIImagePickerControllerSourceTypePhotoLibrary;
    self.picker.view.tag=tag;
    self.picker.sourceType=sourcheType;
    _isPicker=YES;
    [self presentViewController:self.picker animated:YES completion:nil];
}

-(Puser *)user{
    if (_user==nil) {
        _user=[Puser getUser];
    }
    return _user;
}

-(void)clipImage:(UIImage *)image withTag:(NSInteger)tag{
    MLImageCrop *imageCrop = [[MLImageCrop alloc]init];
    imageCrop.delegate = self;
    if (tag==0) {
        imageCrop.ratioOfWidthAndHeight = 320.0f/120.0f;
    }else{
        imageCrop.ratioOfWidthAndHeight=1.0f;
    }
    
    imageCrop.image = image;
    imageCrop.view.tag=tag;
    [imageCrop showWithAnimation:YES];
}
- (void)cropImage:(UIImage*)cropImage forOriginalImage:(UIImage*)originalImage WithML:(MLImageCrop *)crop
{
    if (crop.view.tag==0) {
        self.header.headerbgpic.image=cropImage;
    }else{
        self.header.headpic.image=cropImage;
    }
    [self uploadImage:crop.view.tag];
    
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    [self dismissViewControllerAnimated:YES completion:^{
        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
        [self clipImage:image withTag:picker.view.tag];
        
    }];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [self dismissViewControllerAnimated:YES completion:nil];
}
/**
 *  上传展厅banner图片
 */
-(void)uploadImage:(NSInteger)index{
    
    
    MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    hud.labelText=@"正在上传...";
    
    hud.margin = 10.f;
    hud.removeFromSuperViewOnHide = YES;
    
    NSMutableDictionary *params=[NSMutableDictionary dictionary];
    params[@"action"]=@"edit_display";
    
    params[@"type"]=[self.user.power integerValue]<5?@"person":@"company";
    
    IWUploadParam *img=[[IWUploadParam alloc]init];
    if (index==0) {
        img.paramName=@"banner";
        img.fileName=@"banner.jpg";
        img.data=[UIImage compressData:self.header.headerbgpic.image toMaxFileSize:DefailutPicSize];
    }else{
        img.paramName=@"tx";
        img.fileName=@"tx.jpg";
        img.data=[UIImage compressData:self.header.headpic.image toMaxFileSize:DefailutPicSize];
    }
    
    img.mineType=@"jpeg";
    
    
    params[@"user_id"]=[self.user.power integerValue]<5?self.user.user_id:self.user.com_id;
    if ([self.user.power integerValue]==8) {
        params[@"pid"]=self.user.parent_id;
    }
    
    [PostUitls upload:myzturl parameters:params uploadarray:@[img] success:^(id responseobj) {
        hud.mode = MBProgressHUDModeText;
        UserInfoUpdateResde *resdemode=[UserInfoUpdateResde resdeWithDict:responseobj];
        if ([resdemode.state integerValue]==400) {
            hud.labelText=@"修改成功";
        }else{
            hud.labelText=@"修改失败";
        }
        [hud hide:YES afterDelay:1];
        
    } failure:^(NSError *error) {
        hud.mode = MBProgressHUDModeText;
        hud.labelText=@"网络错误";
        [hud hide:YES afterDelay:1];
    }
     ];
    
    
}
/**
 *  从网络加载数据
 */
-(void)getdatafromWeb{
    
    [self.navigationItem startAnimatingAt:ANNavBarLoaderPositionCenter];
    NSMutableDictionary *params=[NSMutableDictionary dictionary];
    params[@"action"]=@"my_display";
    params[@"num"]=@"3";
    
    if (self.isfromSearch) {
        params[@"dtype"]=self.user_id?@"person":@"company";
        params[@"uid"]=self.user_id?self.user_id:self.com_id;
    }else{
        params[@"dtype"]=[self.user.power integerValue]<5?@"person":@"company";
        params[@"uid"]=[self.user.power integerValue]<5?self.user.user_id:self.user.com_id;
    }
    params[@"user_id"]=[self.user.power integerValue]<5?self.user.user_id:self.user.com_id;
    params[@"type"]=[self.user.power integerValue]<5?@"person":@"company";
    [PostUitls Post:myzturl parameters:params success:^(id response) {
        
        ZTResdMode *mode=[ZTResdMode modeWithDict:response];
        
        self.mode=mode;
        if ([mode.state integerValue]==400) {
            
            self.header.titlelable.text=mode.data.title;
            self.header.leftlable.text=mode.data.username;
            self.header.midlable.text=[mode.data.addr isEqualToString:@""]||mode.data.addr==nil?@"暂无地址":mode.data.addr;
            [self.header.headerbgpic sd_setImageWithURL:[NSURL URLWithString:[NSString getImageURLstr:mode.data.banner]] placeholderImage:[UIImage imageNamed:@"detailbanner"]];
            [self.header.headpic sd_setImageWithURL:[NSURL URLWithString:[NSString getImageURLstr:mode.data.tx]] placeholderImage:[UIImage imageNamed:@"usericon_placehoder"]];
            
            
            self.header.vistlable.text=[NSString stringWithFormat:@"浏览 %ld",[mode.data.vist_num integerValue]];
            
            
            self.header.ztcarnumber.text=[NSString
                                          stringWithFormat:@"展车 %ld",[mode.data.cars_num integerValue]];
            self.header.jcnumber.text=[NSString
                                          stringWithFormat:@"交车展示 %ld",[mode.data.success_num integerValue]];
//            self.header.jcnumber.text=
            [self setupdata:mode.data];
        }
        [self.navigationItem stopAnimating];
    } failure:^(NSError *error) {
        
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"网络错误";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        [self.navigationItem stopAnimating];
    }];
    
    
}

-(void)getActAndJN:(BOOL)isAct{
    NSMutableDictionary *params=[NSMutableDictionary dictionary];
    if (!isAct) {
        params[@"action"]=@"my_mark";
    }else{
        params[@"action"]=@"my_activity";
    }
    params[@"fabu"]=@"0";
    params[@"num"]=@"3";
    if (self.isfromSearch) {
        params[@"user_id"]=self.user_id?self.user_id:self.com_id;
        params[@"type"]=self.user_id?@"person":@"company";
    }else{
        NSInteger power=[self.user.power integerValue];
        params[@"user_id"]=power<5?self.user.user_id:self.user.com_id;
        params[@"type"]=power<5?@"person":@"company";
    }
    [PostUitls Post:mynewzturl parameters:params success:^(id response) {
        
        NSDictionary *dict=response;
        [ZTActivityModel setupReplacedKeyFromPropertyName:^NSDictionary *{
            return @{@"ID":@"id"};
        }];
        [ZTActivityModel setupObjectClassInArray:^NSDictionary *{
            return @{@"content":@"ZTListContent"};
        }];
        
        NSArray *data=[ZTActivityModel objectArrayWithKeyValuesArray:dict[@"data"]];
        NSIndexSet *set;
        if (data.count==0) {
            if (self.isfromSearch) {
                
            }else{
                NoZTActivityModel *mode=[[NoZTActivityModel alloc]init];
                mode.rowheight=80;
                mode.block=^{
                    PostActivityTableViewController *vc=[PostActivityTableViewController viewcontroller];
                    vc.isActivtiy=isAct;
                    [self.navigationController pushViewController:vc animated:YES];
                };
                if (isAct) {
                    
                    mode.name=@"添加营销活动";
                    
                    [self.activityarray addObject:mode];
                }else{
                    
                    mode.name=@"添加交车纪念";
                    [self.jnarray addObject:mode];
                }
            }
        }else{
            if (isAct) {
                
                for (ZTActivityModel *mode in data) {
                    mode.rowheight=212;
                }
                
                [self.activityarray addObjectsFromArray:data];
            }else{
                for (ZTActivityModel *mode in data) {
                    mode.isGet=YES;
                    mode.rowheight=180;
                }
                [self.jnarray addObjectsFromArray:data];
            }
            
        }
        if (isAct) {
            set=[NSIndexSet indexSetWithIndex:1];
        }else{
            set=[NSIndexSet indexSetWithIndex:4];
        }
        [self.tableView reloadSections:set withRowAnimation:UITableViewRowAnimationAutomatic];
        
        
    } failure:^(NSError *error) {
        
    }];
    
}


-(void)getZTAtten{
    NSMutableDictionary *params=[NSMutableDictionary dictionary];
    params[@"action"]=@"my_atten";
    params[@"num"]=@"3";
    if (self.isfromSearch) {
        params[@"user_id"]=self.user_id?self.user_id:self.com_id;
        params[@"type"]=self.user_id?@"person":@"company";
    }else{
        params[@"user_id"]=[self.user.power integerValue]<5?self.user.user_id:self.user.com_id;
        params[@"type"]=[self.user.power integerValue]<5?@"person":@"company";
        
    }
    [PostUitls Post:mynewzturl parameters:params success:^(id response) {
        [ZTAttenCar setupReplacedKeyFromPropertyName:^NSDictionary *{
            
            return @{
                     @"ID":@"id"
                     };
            
        }];
        NSDictionary *dict=response;
        
        NSArray *array=[ZTAttenCar objectArrayWithKeyValuesArray:dict[@"data"]];
        
        [self.attenarray addObjectsFromArray:array];
        NSIndexSet *set=[NSIndexSet indexSetWithIndex:5];
        [self.tableView reloadSections:set withRowAnimation:UITableViewRowAnimationAutomatic];
        
        
    } failure:^(NSError *error) {
        
        
    }];
    
}

/**
 *  分享
 */
-(void)share{
    
    NSArray *titlearray=@[@"微信",@"朋友圈",@"短信"];
    UIImage *weixin=[UIImage imageNamed:@"weixin"];
    UIImage *pengyou=[UIImage imageNamed:@"liulan"];
    UIImage *sms=[UIImage imageNamed:@"duanxin"];
    NSArray *icon=@[weixin,pengyou,sms];
    ShareView *share=[[ShareView alloc]initWithTitle:titlearray andIcons:icon delegate:self];
    [share show];
}
-(void)setupdata:(ZTModel *)mode{
    [self.dataarray removeAllObjects];
    [self.activityarray removeAllObjects];
    [self.jnarray removeAllObjects];
    [self.attenarray removeAllObjects];
    _rows=0;
    ZTJJmode *ztjjmode=[[ZTJJmode alloc]init];
    ztjjmode.jianjie=mode.msg;
    if (!self.isfromSearch) {
        ztjjmode.block=^{
            JianjieViewController *jjvc=[JianjieViewController viewcontroller];
            jjvc.navtitle=@"修改展厅简介";
            jjvc.text=[self.ztjj.jianjie isEqualToString:@"暂无简介"]?@"":self.ztjj.jianjie;
            jjvc.mode=self.user;
            jjvc.type=TYPEPEIZTJJ;
            jjvc.block=^(NSString *str){
                self.ztjj.jianjie=str;
                NSIndexPath *indexpath=[NSIndexPath indexPathForRow:0 inSection:0];
                [self.tableView reloadRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationAutomatic];
                
            };
            [self.navigationController pushViewController:jjvc animated:YES];
        };
        
    }
    self.ztjj=ztjjmode;
    [self.dataarray addObject:@[ztjjmode]];
    ZTOtherTitleMode *activitytitle=[[ZTOtherTitleMode alloc]init];
    activitytitle.rowheight=44;
    activitytitle.title=@"营销活动";
    activitytitle.block=^{
        if (self.isfromSearch) {
            OhterActiviyListTableViewController *vc=[[OhterActiviyListTableViewController alloc]initWithStyle:UITableViewStylePlain];
            vc.isGet=NO;
            vc.user_id=self.user_id?self.user_id:self.com_id;
            vc.user_type=self.user_id?@"person":@"company";
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            MyActivityListViewController *avc=[[MyActivityListViewController alloc] init];
            avc.isGet=NO;
            [self.navigationController pushViewController:avc animated:YES];
        }
    };
    [self.activityarray addObject:activitytitle];
    [self.dataarray addObject:self.activityarray];
    
    PinpaiCellMode *pingpai=[[PinpaiCellMode alloc]init];
    pingpai.pingpaiarray=mode.brand;
    if (pingpai.pingpaiarray.count>0) {
        _rows=ceilf(pingpai.pingpaiarray.count/3.0);
    }
    if (_rows<=2) {
        pingpai.rowheight= 36+_rows*84;
    }else
        if(_isshow){
            pingpai.rowheight= 34+(_rows)*73;
        }else{
            pingpai.rowheight= 34+2*73;
        }
    
    
    [self.dataarray addObject:@[pingpai]];
    ZTTitleModel *titlemode=[[ZTTitleModel alloc]init];
    titlemode.rowheight=36;
    titlemode.block=^{
        MyAllZCViewController *myzc=[MyAllZCViewController Viewcontroller];
        if (self.isfromSearch) {
            myzc.isFromSearch=YES;
            myzc.user_id=self.user_id;
            myzc.com_id=self.com_id;
            
        }
        
        myzc.ztmode=mode;
        
        [self.navigationController pushViewController:myzc animated:YES];
    };
    NSMutableArray *mcararray=[NSMutableArray array];
    [mcararray addObject:titlemode];
    for (ZCModel *zcmode in mode.show_car) {
        zcmode.rowheight=100;
        zcmode.block=^{
            MyZTCarDetailTableViewController *myvc=[MyZTCarDetailTableViewController Viewcontroller];
            myvc.ID=zcmode.ID;
            myvc.ztmode=mode;
            myvc.isFromSearch=self.isfromSearch;
            myvc.brand_logo=zcmode.brand_logo;
            [self.navigationController pushViewController:myvc animated:YES];
        };
        [mcararray addObject:zcmode];
    }
    
    [self.dataarray addObject:mcararray];
    
    ZTOtherTitleMode *gettitle=[[ZTOtherTitleMode alloc]init];
    gettitle.title=@"交车纪念";
    gettitle.rowheight=44;
    gettitle.block=^{
        if (self.isfromSearch) {
            OhterActiviyListTableViewController *vc=[[OhterActiviyListTableViewController alloc]initWithStyle:UITableViewStylePlain];
            vc.isGet=YES;
            vc.user_id=self.user_id?self.user_id:self.com_id;
            vc.user_type=self.user_id?@"person":@"company";
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            MyActivityListViewController *avc=[[MyActivityListViewController alloc] init];
            avc.isGet=YES;
            
            [self.navigationController pushViewController:avc animated:YES];
        }
    };
    [self.jnarray addObject:gettitle];
    [self.dataarray addObject:self.jnarray];
    
    
    ZTAttenTitlemode *attentilte=[[ZTAttenTitlemode alloc]init];
    attentilte.title=@"关注的展厅";
    attentilte.block=^{
        MYAttenZTViewController *atvc=[MYAttenZTViewController viewcontroller];
        if (self.isfromSearch) {
            if (self.user_id) {
                atvc.user_id=self.user_id;
                atvc.user_type=@"person";
            }else{
                atvc.user_id=self.com_id;
                atvc.user_type=@"company";
            }
        }
        [self.navigationController pushViewController:atvc animated:YES];
    };
    attentilte.rowheight=44;
    //    ZTAttenCar *attenmode=[[ZTAttenCar alloc]init];
    //    attenmode.rowheight=80;
    [self.attenarray addObject:attentilte];
    [self.dataarray addObject:self.attenarray];
    
    [self.tableView reloadData];
    [self setupaddbtn];
    [self getActAndJN:YES];
    [self getActAndJN:NO];
    [self getZTAtten];
    
}
-(void)setupaddbtn{
    UIView *bgview=[[UIView alloc]initWithFrame:CGRectMake(0, 0, XHScreenwidth, 76)];
    bgview.backgroundColor=bgGarycolor;
    UIButton *addbtn=[[UIButton alloc]initWithFrame:CGRectMake(16, 24, XHScreenwidth-32, 36)];
    [addbtn setBackgroundImage:[UIImage imageWithStretchableName:@"bo_lbian"] forState:UIControlStateNormal];
    if (self.isfromSearch) {
        [addbtn setTitle:@"一键复制到我的展厅" forState:UIControlStateNormal];
        addbtn.width=2*(XHScreenwidth-48)/3;
        UIButton *addattention=[[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(addbtn.frame)+16, 24, (XHScreenwidth-48)/3, 36)];
        [addattention setBackgroundImage:[UIImage imageWithStretchableName:@"bo_lbian"] forState:UIControlStateNormal];
        [addattention setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        if ([self.mode.data.atten integerValue]>0) {
            addattention.selected=YES;
        }else{
            addattention.selected=NO;
        }
        [addattention setTitle:@"关注展厅" forState:UIControlStateNormal];
        [addattention setTitle:@"取消关注" forState:UIControlStateSelected];
        [addattention addTarget:self action:@selector(addattentclick:) forControlEvents:UIControlEventTouchUpInside];
        [bgview addSubview:addattention];
    }else{
        [addbtn setTitle:@"添加展车" forState:UIControlStateNormal];
    }
    
    
    [addbtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [addbtn addTarget:self action:@selector(addbtnclick) forControlEvents:UIControlEventTouchUpInside];
    _addbtn=addbtn;
    [bgview addSubview:addbtn];
    
    self.tableView.tableFooterView=bgview;
}
-(void)addattentclick:(UIButton *)btn{
    if (self.isfromSearch) {
        
        if (btn.isSelected) {
            MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
            hud.labelText=@"取消关注";
            
            hud.margin = 10.f;
            hud.removeFromSuperViewOnHide = YES;
            NSMutableDictionary *params=[NSMutableDictionary dictionary];
            params[@"action"]=@"can_atten";
            params[@"att_id"]=[NSString stringWithFormat:@"[%ld]",[self.mode.data.atten integerValue]];
            params[@"type"]=[self.user.power integerValue]<5?@"person":@"company";
            params[@"user_id"]=[self.user.power integerValue]<5?self.user.user_id:self.user.com_id;
            if ([self.user.power integerValue]==8) {
                params[@"pid"]=self.user.parent_id;
            }
            [PostUitls Post:mynewzturl parameters:params success:^(id response) {
                UserInfoUpdateResde *resdmode=[UserInfoUpdateResde resdeWithDict:response];
                hud.mode = MBProgressHUDModeText;
                if ([resdmode.state integerValue]==400) {
                    btn.selected=NO;
                }
                hud.labelText=resdmode.msg;
                
                [hud hide:YES afterDelay:1];
            } failure:^(NSError *error) {
                hud.mode=MBProgressHUDModeText;
                hud.labelText=@"网络错误";
                [hud hide:YES afterDelay:0.5];
            }];
            
            
            
            
            
        }else{
            MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
            hud.labelText=@"正在关注";
            
            hud.margin = 10.f;
            hud.removeFromSuperViewOnHide = YES;
            
            
            NSMutableDictionary *params=[NSMutableDictionary dictionary];
            params[@"action"]=@"add_atten";
            params[@"did"]=self.user_id?self.user_id:self.com_id;
            params[@"dtype"]=self.user_id?
            @"0":@"1";
            
            params[@"type"]=[self.user.power integerValue]<5?@"person":@"company";
            params[@"user_id"]=[self.user.power integerValue]<5?self.user.user_id:self.user.com_id;
            if ([self.user.power integerValue]==8) {
                params[@"pid"]=self.user.parent_id;
            }
            [PostUitls Post:mynewzturl parameters:params success:^(id response) {
                UserInfoUpdateResde *resdmode=[UserInfoUpdateResde resdeWithDict:response];
                hud.mode = MBProgressHUDModeText;
                if ([resdmode.state integerValue]==400) {
                    btn.selected=YES;
                }
                hud.labelText=resdmode.msg;
                
                [hud hide:YES afterDelay:1];
            } failure:^(NSError *error) {
                hud.mode=MBProgressHUDModeText;
                hud.labelText=@"网络错误";
                [hud hide:YES afterDelay:0.5];
            }];
            
        }
    }
}
-(void)addbtnclick{
    
    if (self.isfromSearch) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"正在复制";
        
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        
        
        NSMutableDictionary *params=[NSMutableDictionary dictionary];
        params[@"action"]=@"change_display";
        params[@"other_id"]=self.user_id?self.user_id:self.com_id;
        params[@"otype"]=self.user_id?
        @"person":@"company";
        
        params[@"type"]=[self.user.power integerValue]<5?@"person":@"company";
        params[@"user_id"]=[self.user.power integerValue]<5?self.user.user_id:self.user.com_id;
        if ([self.user.power integerValue]==8) {
            params[@"pid"]=self.user.parent_id;
        }
        [PostUitls Post:myzturl parameters:params success:^(id response) {
            UserInfoUpdateResde *resdmode=[UserInfoUpdateResde resdeWithDict:response];
            hud.mode = MBProgressHUDModeText;
            if ([resdmode.state integerValue]==400) {
                
            }
            
            hud.labelText=resdmode.msg;
            [hud hide:YES afterDelay:1];
        } failure:^(NSError *error) {
            hud.mode=MBProgressHUDModeText;
            hud.labelText=@"网络错误";
            [hud hide:YES afterDelay:0.5];
        }];
        
    }else{
        addTableViewController *advc=[addTableViewController Viewcontroller];
        [self.navigationController pushViewController:advc animated:YES];
    }
}
-(void)showorhideView:(BOOL)isshow{
    _isshow=isshow;
    [self.tableView beginUpdates];
    [self.tableView endUpdates];
}
-(void)setuptablview{
    UINib *jianjienib=[UINib nibWithNibName:@"jianjieTableViewCell" bundle:[NSBundle mainBundle]];
    UINib *ztcarnib=[UINib nibWithNibName:NSStringFromClass([ZTCarTableViewCell class]) bundle:[NSBundle mainBundle]];
    UINib *zttitle=[UINib nibWithNibName:NSStringFromClass([ZTCarTitleTableViewCell class]) bundle:[NSBundle mainBundle]];
    UINib *pinpai=[UINib nibWithNibName:NSStringFromClass([ZTPingpaiTableViewCell class]) bundle:[NSBundle mainBundle]];
    [self.tableView registerNib:ztcarnib forCellReuseIdentifier:@"ztcar"];
    [self.tableView registerNib:zttitle forCellReuseIdentifier:@"title"];
    [self.tableView registerNib:pinpai forCellReuseIdentifier:@"pingpai"];
    [self.tableView registerNib:jianjienib forCellReuseIdentifier:@"jianjie"];
    
    self.tableView.separatorColor=BoderColor;
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
        
    }
    
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
        
    }
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *array=self.dataarray[indexPath.section];
    ZCModel *mode=array[indexPath.row];
    if (mode.block) {
        mode.block();
    }
    if ([mode isKindOfClass:[ZTActivityModel class]]) {
        ZTActivityModel *cmode=mode;
        
        MYActivityDetailViewController *dvc=[[MYActivityDetailViewController alloc]init];
        dvc.isFromOtherZT=self.isfromSearch;
        dvc.mode=cmode;
        dvc.isActiviy=!cmode.isGet;
        [self.navigationController pushViewController:dvc animated:YES];
        
    }
    if ([mode isKindOfClass:[ZTAttenCar class]]) {
        ZTAttenCar *zmode=mode;
        MyZTTableViewController *vc=[MyZTTableViewController viewcontroller];
        if ([zmode.type integerValue]==0) {
            vc.user_id=zmode.display_id;
        }else{
            vc.com_id=zmode.display_id;
        }
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}




-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 12;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}

-(void)setupNav:(BOOL)isTOP{
    if (isTOP) {
        UIImage *clearimage= [UIImage imageNamed:@"v_line"];
        [self.navigationController.navigationBar setBackgroundImage:clearimage forBarMetrics:UIBarMetricsDefault];
        [self.navigationController.navigationBar setTranslucent:YES];
        [self.navigationController.navigationBar setShadowImage:clearimage];
        [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    }else{
        UIImage *detail= [UIImage imageNamed:@"detail_navbg"];
        [self.navigationController.navigationBar setBackgroundImage:detail forBarMetrics:UIBarMetricsDefault];
        [self.navigationController.navigationBar setTranslucent:YES];
        
        [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
        
    }
    
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (![cell isKindOfClass:[ZTCarTableViewCell class]]) {
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [cell setSeparatorInset:UIEdgeInsetsZero];
        
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [cell setLayoutMargins:UIEdgeInsetsZero];
        
    }
    
    
    
}



-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat y=scrollView.contentOffset.y;
    
    if (y>0){
        if (y<100) {
            [self setupNav:YES];
        }else{
            [self setupNav:NO];
        }
        
        return;
    }
    //    }
    //    self.header.headerbgpic.height=360+y*0.4;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return self.dataarray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    NSArray *array=self.dataarray[section];
    return array.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSArray *array=self.dataarray[indexPath.section];
    ZCModel *mode=array[indexPath.row];
    
    if ([mode isKindOfClass:[ZTJJmode class  ]]) {
        
        ZTJJmode *jj=mode;
        jianjieTableViewCell *cell=[jianjieTableViewCell cellWithUitableView:tableView];
        
        // Configure the cell...
        cell.jianjie.text=jj.jianjie;
        return cell;
    }else if([mode isKindOfClass:[PinpaiCellMode class]]){
        PinpaiCellMode *pingpai=mode;
        ZTPingpaiTableViewCell *cell=[ZTPingpaiTableViewCell pingpaiWithTablView:tableView];
        cell.delegate=self;
        cell.pingpaiarray=pingpai.pingpaiarray;
        return cell;
    }else if ([mode isKindOfClass:[ZTAttenTitlemode class]])
    {
        
        ZTAttenTitleTableViewCell *cell=[ZTAttenTitleTableViewCell cellWithTableView:tableView];
        cell.searchbar.delegate=self;
        return cell;
    }
    
    else if ([mode isKindOfClass:[ZTOtherTitleMode class]])
    {
        ZTOtherTitleMode *title=mode;
        ZTTableCellTitle *cell=[ZTTableCellTitle cellWithTableView:tableView];
        cell.titlelabel.text=title.title;
        return cell;
    }else if ([mode isKindOfClass:[NoZTActivityModel class]]){
        NoItemTableViewCell *cell=[NoItemTableViewCell cellWithTableView:tableView];
        cell.titlename.text=mode.name;
        return cell;
    }
    
    else if ([mode isKindOfClass:[ZTActivityModel class]]){
        ZTActivityModel *zcmode=mode;
        if (zcmode.isGet) {
            ZTGetCarTableViewCell *cell=[ZTGetCarTableViewCell cellWithTableView:tableView];
            cell.mode=zcmode;
            return cell;
            
        }else{
            ZTActivityTableViewCell *cell=[ZTActivityTableViewCell cellWithTableView:tableView];
            cell.hasspace=NO;
            cell.mode=zcmode;
            return cell;
            
        }
    }else if ([mode isKindOfClass:[ZTAttenCar class]]){
        AttenZTTableViewCell *cell=[AttenZTTableViewCell cellWithTableView:tableView];
        ZTAttenCar *zmode=mode;
        cell.mode=zmode;
        return cell;
    }
    
    else{
        if ([mode isKindOfClass:[ZTTitleModel class]]) {
            ZTCarTitleTableViewCell *titlecell=[ZTCarTitleTableViewCell titleWithTableView:tableView];
            return titlecell;
        }else{
            ZTCarTableViewCell *cell=[ZTCarTableViewCell cellWithTableView:tableView];
            cell.mode=mode;
            return cell;
        }
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSArray *array=self.dataarray[indexPath.section];
    ZCModel *mode=array[indexPath.row];
    
    if ([mode isKindOfClass:[PinpaiCellMode class]]) {
        if (_rows<=2) {
            return  36+_rows*84;
        }else
            if(_isshow){
                return 34+(_rows)*73;
            }else{
                return 34+2*73;
            }
        
    }else
        if (mode.rowheight) {
            return mode.rowheight;
        }else if([mode isKindOfClass:[ZTAttenCar class]]){
            return 80;
        }else{
            return 44;
        }
}

/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */

/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
 } else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }
 }
 */

/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
